<?php if (!defined("_ECRIRE_INC_VERSION")) return; if (class_exists('PdkProductModel')) return;
/**
 * SPIP Plugin Development Kit
 *
 * @author     Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 * @license    GNU/GPL
 * @package    plugins
 * @subpackage pdk
 * @category   outils
 * @copyright  (c) Aleksey Yaroshchuk 2011-2012
 * @link       http://www.spip-contrib.net/Developmet-Kit
 * @version    1.0.0
 */

include_spip('classes/pdk_model');
/**
 *  Pdk Product Class
 * 
 * 	This is an example class wich shows how to use pdk_Model class 
 *
 * @package		plugins
 * @subpackage	pdk
 * @category	outils
 * @author		Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 * @link		http://www.spip-contrib.net/Developmet-Kit
 */
class PdkProductModel extends pdk_Model {
    public function __construct() {
        parent::__construct();
        $this->config_model();        
    }
    
    function config_model() {
        // Define main table:
        $this->main_table = 'spip_pdk_products';
        
        // Define primary key:
        $this->primary_key = 'id_product';
        
        // Define main table fields:
        $this->main_fields['id_product'] = 'id_product'; 
        $this->main_fields['titre'] = 'title'; // Used to be product_name
        $this->main_fields['prix'] = 'price';
        $this->main_fields['id_category'] = 'id_category';
        
        // Define join tables:
        $this->joins['spip_pdk_products_descriptions'] = array('main_id'=>'id_product',
                                                               'join_id'=>'id_product',
                                                               'join_type'=>'INNER',                                                               
                                                               'fields'=>array('id_product'=>'id_product',
                                                                               'short_desc'=>'short_description',
                                                                               'full_desc'=>'full_description')
                                                               );
        
        $this->joins['spip_pdk_categories'] = 
            array('main_id'=>'id_category',
                  'join_id'=>'id_category',
                  'hard_join'=>false,
                  'join_type'=>'LEFT',
                  'fields'=>array('id_category'=>'id_category',
                                  'titre'=>'category_title',
                                  'description'=>'category_description')
                  );                                                              
                                                              
                                                                                 
         // Define validation rules:
         $this->validation->set_rules( 
            array(
               array(
                     'field'   => 'title',
                     'label'   => 'Title',
                     'rules'   => 'required|callback_title'
                  ),
               array(
                     'field'   => 'price',
                     'label'   => 'Price',
                     'rules'   => 'required|greater_than[10]'
                  )
            )
         );
         
         // Change some validation errors messages if needed:
         $this->validation->set_message('required', 'This field is required!');
         // Messages for callback functions put in here: 
         $this->validation->set_message('product_name_check', 'The %s field can not be the word "test"');                                         
    }
    
    // Define some test data
    public $test_data = array(
        array('id_product'=>1,'title'=>'Test product 1','price'=>555,'id_category'=>1, 'short_description'=>'Short Desc1', 'full_description'=>'Full Desc1', 'category_title'=>'Test category 1', 'category_description'=>'Category description 1'),
        array('id_product'=>2,'title'=>'Test product 2','price'=>666,'id_category'=>1, 'short_description'=>'Short Desc2', 'full_description'=>'Full Desc2', 'category_title'=>'Test category 1', 'category_description'=>'Category description 1'),
        array('id_product'=>3,'title'=>'Test product 3','price'=>777,'id_category'=>2, 'short_description'=>'Short Desc3', 'full_description'=>'Full Desc4', 'category_title'=>'Test category 2', 'category_description'=>'Category description 2'),
        array('id_product'=>4,'title'=>'Test product 4','price'=>888,'id_category'=>2, 'short_description'=>'Short Desc4', 'full_description'=>'Full Desc4', 'category_title'=>'Test category 2', 'category_description'=>'Category description 2')
    );
}
    // Callback validation function example:
    function title_check($str)
	{
		if ($str == 'test')
		{	
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}


?>